import StrTo from './StrTo.js'
import Row  from "./Row.js"
import  Blocks from "./Blocks.js"
//import StroTo from "../../../../../../../../editor/src/main/js/src/StrTo";

/**
 * 简单打包解析数据
 */
export default class Jsr{
    /**
     * 第一个块是用来装载基本信息，
     * 比如，这个模块叫什么，
     * 目录起始块到结束块，（通常目录丢到最后）
     */
    constructor(){
        let buff=new Uint8Array(1250)
        this.list=[ buff];
        this.byteOffset=0;
        this.length=1;
        this.paths=new Blocks();
    }

    /**
     *  加载 数据
     * @param data
     */
    loadData(data){
        this.list=new Array();
        let length=0;
        let buff= new Uint8Array(data)
        if(data){
            while (buff.byteLength-1250*length>0){
                let uint8array1=new Uint8Array(1250);
                uint8array1.set(buff.subarray(length*1250, length*1250+1250))
                this.list.push(uint8array1 )
                length++;
            }
        }
        this.length=length;
        this.  analysisHead(); //解析 头模块
        this.analysisIndexes();   // 解析 索引
    }
    /**
     * 获取全部数据
     * @returns {uint8array[]|*[]}
     */
    findAllBuff(){

        let suoyin=StrTo.utf8ToNum("索引");

        let paths=this.paths;
        let pathsBuff=paths.encoded();
        this.list.push(...pathsBuff)

        let  blocks=new Blocks();
        blocks.addRow(new Row(suoyin,this.length,(this.length+pathsBuff.length),suoyin))
        let synopsisBuff= blocks.encoded();
        this.list[0]=synopsisBuff[0]
        return this.list;
    }
    /**
     * 解析索引
     * @returns {*|number|void}
     */
    analysisHead(){
        let synopsisBuff   = this.list[0];
        let blocks=new Blocks();
        blocks.decode([synopsisBuff]);
        this.head=blocks.findBlock(1);
    }
    /**
     * 解析目录
     * @returns {*|number|void}
     */
    analysisIndexes(){
        let path=   this.head.findRow(1);
        let pathBuff=this.list.slice(parseInt(path.find(2)),parseInt(path.find(3)));
        let blocks=new Blocks();
        blocks.decode(pathBuff);
        this.paths=blocks;
    }

    /**
     *  通过正则获取路径 返回单个文件的路径
     *  暂时只有路径和文件命名有效
     * @param path
     * @returns {*}
     */
    findPathByRegular( jsr,path,fileName){
        if(path&&fileName){ //路径和文件名都存在
            let regPath=new  RegExp(path);
            let regFile=new RegExp(fileName);
            for(let i=0;i<this.paths.length;i++){
                let block= this.paths.findBlock(i+1);
                let numPath=  block.findRow(1).find(3);
                let testPath=StrTo.numToUtf8(numPath);
                var obj=str.lastIndexOf("/");
                let textFileName=testPath.substr(obj+1);
                let textPathName=testPath.substr(0,obj);

                if(  regPath.test(textPathName)&&regFile.test(textFileName)){
                    return block;
                }
            }
        }else if (path&&!fileName){ //路径存在
            let regPath=new  RegExp(path);
            for(let i=0;i<this.paths.length;i++){
                let block= this.paths.findBlock(i+1);
                let numPath=  block.findRow(1).find(3);
                let testPath=StrTo.numToUtf8(numPath);
                var obj=str.lastIndexOf("\\");
                let textPathName=testPath.substr(0,obj);

                if(  regPath.test(textPathName)){
                    return block;
                }
            }
        }else if(!path&&fileName){ //文件名存在
            let regFile=new RegExp(fileName);
            for(let i=0;i<this.paths.length;i++){
                let block= this.paths.findBlock(i+1);
                let numRow=  block.findRow(1)
                let  numPath=  numRow .find(3);
                if(numPath){
                    let testPath=StrTo.numToUtf8(numPath);
                    var obj=testPath.lastIndexOf("\\");
                    let textFileName=testPath.substr(obj+1);

                    if( regFile.test(textFileName)){
                        return block;
                    }
                }

            }
        }
        return null;
    }
    /**
     * 根据路径获取文件
     * @returns {Array}
     * @private
     */
    findFileByRegular(jsr,path,fileName){
        let _this=this;
        let pathRow=_this.findPathByRegular(jsr,path,fileName);

        if(pathRow!=null){
            let row=pathRow.findRow(2);
            let listBuff =_this.list.slice(row.find(1),row.find(2))

            let resArr=[];
            let yu=row.find(3)/2;
            for(let i=0;i<listBuff.length;i++){

                if(i==listBuff.length-1){
                    let list=listBuff[i].slice(0,yu);
                    resArr.push(...list)
                }else {
                    resArr.push(...listBuff[i]);
                }
            }
            let res = new Uint8Array(resArr);
            return res ;


        }

        return null;
    }

    /**
     *  通过路径 获取路径的详细信息
     * @param path
     * @returns {*}
     */
    findPath( path){
        let pathNum=StrTo.utf8ToNum(path);
        for(let i=0;i<this.paths.length;i++){
            let block= this.paths.findBlock(i+1);
            if(   block.findRow(1).find(3)==pathNum){
                return block;
            }
        }
        return null;
    }




    /**
     * 添加数据
     * @param data
     * @param path
     */
    pushFile(data,path,type){
        let length=0;
        let start=this.length;
        let buff= new Uint8Array(data);
        if(data){
            while (buff.byteLength-1250*length>0){
                let uint8array1=new Uint8Array(1250);
                uint8array1.set(buff.subarray(length*1250, length*1250+1250))
                this.list.push(uint8array1 )
                length++;
            }
        }
        let yu= buff.byteLength%1250
        let end=this.length+length;

        yu=yu*2;

        let pathNum=StrTo.utf8ToNum(path);
        let typeNum=StrTo.utf8ToNum(type);
        let bian=StrTo.utf8ToNum("长度");
        let path1=new Row("","",pathNum,typeNum);
        let num =new Row(start,end,yu,bian)

        this.paths.addBlock();
        this.paths.addRow(path1);
        this.paths.addRow(num);

        this.length+=length;

    }


    /**
     * 根据路径获取文件
     * @returns {Array}
     * @private
     */
    findFile(path){
        let _this=this;
        let pathRow=_this.findPath(path);

        if(pathRow!=null){
            let row=pathRow.findRow(2);
            let listBuff =_this.list.slice(row.find(1),row.find(2))

            let resArr=[];
            let yu=row.find(3)/2;
            for(let i=0;i<listBuff.length;i++){

                if(i==listBuff.length-1){
                    let list=listBuff[i].slice(0,yu);
                    resArr.push(...list)
                }else {
                    resArr.push(...listBuff[i]);
                }
            }
            let res = new Uint8Array(resArr);
            return res ;


        }

        return null;
    }





}

